﻿function a(ajax_dest,fic_source,import_file,image_load) {    
    $('document').ready(function () {        
        $('#tof img').click(function (e) {
            $('#tof_file').click();
            e.preventDefault();
        });
        $('#tof_file').change(function (e) {
            var fic = e.target.files[0];
            if (fic.type == 'image/jpeg' || fic.type == 'image/png') {
                $('#image_erreur').html('');
                var form_ = new FormData();
                form_.append(fic.name, fic);
                $.ajax({
                    url: ajax_dest,
                    type: 'POST',
                    contentType: false,
                    processData: false,
                    data: form_,
                    success: function (result) {
                        //alert('typeof=' + typeof (result.isValid));
                        if (result.isValid == true) {
                            $('#tof img').attr('src', fic_source + '' + result.FileName);
                            var w, h;
                            w = parseInt($('#tof img').width());
                            h=parseInt($('#tof img').height());                           
                            h = Math.floor(h * 1.17);
                            $('#tof img').width(w); $('#tof img').height(h);
                        }
                        else {
                            $('#tof img').attr('src', fic_source + 'no_image.jpg');
                            $('#tof img').width('198');// $('#tof img').height('160');
                            $('.image_size').html('<span>L\'image est trop grande</span>');
                        }
                    },
                    error: function () {
                        alert('Ajax erreur');
                    }
                });
            }
            else {
                $('#image_erreur').html('Veuillez inserer une image de format jpg ou png');
                e.preventDefault();
            }
        });
        $('#Nom').keyup(function () {
            $(this).val($(this).val().toUpperCase());
        });
        $('#Prenom').keyup(function (e) {

        });
        $('#Prenom').blur(function () {
            if ($(this).val().trim() != '') {
                var texte = $(this).val().trim().split(' ');
                for (var i = 0; i < texte.length; i++) {
                    if (texte[i].charCodeAt(0) >= 97 && texte[i].charCodeAt(0) <= 123) {
                        var first = String.fromCharCode(texte[i].charCodeAt(0) - 32)
                        texte[i] = first + texte[i].substring(1, texte[i].length);
                    }
                }
                $(this).val(texte.join(' '));
            }
        });
        //Evénements liés importation fichier excel
        //var load = $('#load');        
        $('#ii').click(function () {            
            $('#import_file').click();
        });
        $('#import_file').change(function (evt) {
            var file = evt.target.files[0];            
            $('#load').addClass('ajax'); $('#load').append(image);
            var conteneur = $('#load').get(0).getBoundingClientRect();
            $('#ajax_image').css('top', Math.floor(conteneur.height/2)); $('#ajax_image').css('left',Math.floor(conteneur.width/2));
            //alert($('#load').html());
            if (file.type == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') {
                var data = new FormData();
                data.append('dataSheet', file);
                $.ajax({
                    cache:false,
                    data: data,
                    type: 'POST',
                    url: import_file,
                    contentType: false,
                    processData:false,
                    success: function (result) {
                        alert(result.Status);
                        $('#load').removeClass('ajax'); $('#load').html('');
                    },
                    error: function () {
                        console.log('Erreur');
                    }
                });
            }
            else
                alert('Vous ne pouvez pas importer ce type de fichier');
        });
    });
}
